package utils;

import io.PermutationReader;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

public class SequenceDriver {
	private PermutationReader reader;
	private PrintWriter wr;
	private SequenceDatabase db;
	private Shuffler shuffler;

	public SequenceDriver(SequenceDatabase database, InputStream stream,
			String outputFile) throws IOException {
		shuffler=new Shuffler();
		wr = new PrintWriter(new BufferedWriter(new FileWriter(outputFile)));
		reader = new PermutationReader(stream);
		db = database;
		System.out.println("Starting to analyze");
		analyze();
		reader.close();

	}

	private void analyze() throws IOException {
		String permutation = "";
		int mone;
		int s=0;
		while ((permutation = reader.getNext()) != null) {
			mone = 0;
			for (int i = 0; i < db.size(); i++) {
				String sequence=db.getSequence(i);
				sequence = shuffler.shuffle(sequence);
				if (sequence.indexOf(permutation) != -1) {
					mone++;
				}
			}
			wr.println(permutation + " , " + mone);
			if (s%100==0) System.out.println("Permutation "+ s);
			s++;
		}
		wr.close();

	}
}
